import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Vector;



//@Uses: A class containig utility function 
public class utility 
{
	
/* A function that calculate  the frequency of each Ngram
	(For calcultaing length and normalization)*/
public Vector<ngram_and_frq> cal_unique_ngram_with_frq(String[] ngrams) 
{
		
   Vector<ngram_and_frq> unique_ngram =  new Vector<ngram_and_frq>();
   
   for(int i=0;i<ngrams.length;i++)
   {
	   String ng = ngrams[i];
	   
	   boolean found =  false;
	   for(int j=0;j<unique_ngram.size();j++)
	   {
		   String ung =  unique_ngram.get(j).ngram;
		   if(ng.equalsIgnoreCase(ung))
		   {
			   int ung_frq = unique_ngram.get(j).frq;
			   ung_frq++;
			   
			   ngram_and_frq temp = new ngram_and_frq(ung,ung_frq);
			   unique_ngram.remove(j);
			   unique_ngram.add(j, temp);			   
			   found =  true;
			   break;
		   }//if
		   
	   }//for	   
	   
	   if(!found)
	   { 
		   ngram_and_frq new_temp = new  ngram_and_frq(ng,1); 
		   unique_ngram.add(new_temp);
	   }
	   
   }//for   
   
	return unique_ngram;
}	
	

//get distinct ranked files for a particluar bug   
public ArrayList<file_and_rank> get_ranked_files(String bugid, Connection conn, String score_table, String sum_col) 
{
		ArrayList<file_and_rank> ranked_table_files =  new ArrayList<file_and_rank>();
		
		String  rank_q ="select file_name,"+ sum_col +" from "+ score_table +" where bugid = '"+bugid+"' order by "+ sum_col +" desc,file_name";
		System.out.println("rank_q="+rank_q);
		
		Statement stmt =  null;
		try 
		{
			stmt  = conn.createStatement();
			stmt.execute(rank_q);
			ResultSet rs =  stmt.getResultSet();
			int rank =0;
			while(rs.next())
			{
				rank++;
				String file_name = rs.getString("file_name");
				double score =  rs.getDouble(sum_col);
				ranked_table_files.add(new file_and_rank(file_name,rank,score));
			}
		} catch (SQLException e) 
		{ e.printStackTrace(); }
			
	return ranked_table_files;
 }

//ArrayList containing files related bugs and ranked table file
//used to return sorted list of files related to bug

public ArrayList<file_and_rank> get_ranked_bug_files( ArrayList<file_and_rank> ranked_table_files, ArrayList<String> bug_valid_svn_file) 
{
 
	ArrayList<file_and_rank> ranked_bug_files = new ArrayList<file_and_rank>();
    
	for(int i=0;i<bug_valid_svn_file.size();i++)
    {
    	String file_name = bug_valid_svn_file.get(i);
    	file_name =  file_name.replace(".java", "");
    	for(int j=0;j<ranked_table_files.size();j++)
    	{
    		String table_file = ranked_table_files.get(j).file_name;
    		
    		if(file_name.equalsIgnoreCase(table_file))
    		{
    			int rank = ranked_table_files.get(j).rank;
    			double score = ranked_table_files.get(j).score;
    			file_and_rank obj =  new file_and_rank(file_name,rank,score);
    			ranked_bug_files.add(obj);
    			
    			break;
    			
    		}
    	}
    }//for
 
 
   Collections.sort(ranked_bug_files);
	
   return ranked_bug_files;
   
}

public void print(ArrayList<file_and_rank> ranked_bug_files, String bugid)
{
  System.out.println("======================================");	
  System.out.println("Bugid ="+bugid);	
  
  for(int i=0;i<ranked_bug_files.size();i++)
  {
	  String file = ranked_bug_files.get(i).file_name;
	  int rank  = ranked_bug_files.get(i).rank;
	  System.out.println("file name="+file+ " rank="+rank);
  }
  
  System.out.println("======================================");
	
}//


//To normaliz the score given
public double normalize(double sim_score, Vector<ngram_and_frq> t_d_uniq_ng,Vector<ngram_and_frq> f_p_uniq_ng) 
{
	 double val_td=0;
	 double val_fp=0;
	
	for(int i=0;i<t_d_uniq_ng.size();i++)
	{ 
		double temp_val = t_d_uniq_ng.get(i).frq;
		temp_val =  temp_val*temp_val;		
		val_td=val_td+temp_val;
		//System.out.println("word="+t_d_uniq_ng.get(i).ngram+"  uniq_td="+val_td);
		
	}
	
	//System.out.println("-------------------------");
	
	for(int j=0;j<f_p_uniq_ng.size();j++)
	{ 
		double temp_val = f_p_uniq_ng.get(j).frq;
		temp_val =  temp_val*temp_val;		
		val_fp=val_fp+temp_val;
		//System.out.println("word="+f_p_uniq_ng.get(j).ngram+"  uniq_fp="+val_fp);
	}
	
	val_td = Math.sqrt(val_td);
	val_fp = Math.sqrt(val_fp);
	//System.out.println("sqrt uniqu_td="+val_td);
	//System.out.println("sqrt uniqu_fp="+val_fp);
	
	double denominator = val_td *val_fp;
	
	//System.out.println("Sim score="+sim_score);
	
	sim_score = sim_score/denominator;
	
	//System.out.println("Normailzed(sim score)="+sim_score);
	
	return sim_score;
	
}//

}
